<?php


	Class LoginController{
		
		public function DoControll($aLoginView, $aLoginHandler){
			$output = $aLoginView->AddHeader();
			
			//User is logged in and tries to log out
			if($aLoginHandler->IsLoggedIn()){
				
				if($aLoginView->TriedToLogOut()){
					$aLoginHandler->DoLogout();
					$aLoginView->RemoveLoginCookies();
					$output .= $aLoginView->AddMessageLogout();
				}
			}
	
			else{
				//User wants to log in
				if($aLoginView->TriedToLogIn()){
				
					$temppass = $aLoginHandler->GetTempPassword();
					if($aLoginHandler->DoLogin($aLoginView->GetUserName(),$aLoginView->GetPassword(), $temppass)){
						
						if($aLoginView->ISRememberBox()){
							$aLoginView->SetUsernameCookie();
							$aLoginView->SetPasswordCookie($temppass);
						}
						
						$output .= $aLoginView->AddMessageWelcome();
					}
					else{
						$output .= $aLoginView->AddMessageIncorrectInfo();
						$aLoginView->RemoveLoginCookies();
					}
				}
			}
			
			if($aLoginHandler->IsLoggedIn()){
				$output .= $aLoginView->DoLogoutBox();
			}
			else{
				$output .= $aLoginView->DoLoginBox();
			}
			
			return $output;
		}
	}